<template>
  <el-dialog
    :title="!roomForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="roomForm" :rules="dataRule" ref="roomForm" @keyup.enter.native="roomFormSubmit()" label-width="80px">
      <el-form-item prop="type" label-width="0" class="radio_button">
        <el-radio-group v-model="roomForm.type" size="small">
          <el-radio-button label="楼栋">楼栋</el-radio-button>
          <el-radio-button label="教室">教室</el-radio-button>
          <el-radio-button label="男生宿舍">男生宿舍</el-radio-button>
          <el-radio-button label="女生宿舍">女生宿舍</el-radio-button>
        </el-radio-group>
        
        
      </el-form-item>
    <el-form-item label="房间号" prop="roomNumber">
      <el-input v-model="roomForm.roomNumber" placeholder="房间号"></el-input>
    </el-form-item>
    <el-form-item v-if="roomForm.type!='楼栋'" label="所属楼栋" prop="parentId">
      <el-select class="select" v-model="roomForm.parentId">
        <el-option v-for="item in build" :key="item.id" :label="item.roomNumber" :value="item.id"></el-option>

      </el-select>
      <!-- <el-input v-model="roomForm.parentId" placeholder="所属楼栋"></el-input> -->
    </el-form-item>
   
    <el-form-item  v-if="roomForm.type!='楼栋'" label="容纳人数" prop="capacity">
      <el-input v-model="roomForm.capacity" placeholder="容纳人数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="roomFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { roomApi } from '@/api/baseinfo'

  export default {
    data () {
      return {
        visible: false,
        build:[],
        roomForm: {
          id: 0,
          roomNumber: '',
          parentId: '',
          type: '教室',
          capacity: ''
        },
        dataRule: {
          roomNumber: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '所属楼栋不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          capacity: [
            { required: true, message: '容纳人数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.roomForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.roomForm = this.$options.data().roomForm
          // this.$refs['roomForm'].resetFields()
          roomApi.listbuild().then(({data})=>{
            this.build=data
          })
        })
        if(id){
          roomApi.getRoomInfo(this.roomForm.id).then((data) => {
              if (data && data.code == 20000) {
                this.roomForm = data.room
              }
            })
        }
          
        
      },
      // 表单提交
      roomFormSubmit () {
        this.$refs['roomForm'].validate((valid) => {
          if (valid) {
            roomApi.saveOrUpdate(this.roomForm,!this.roomForm.id ? 'save' : 'update').then((data) => {
              if (data && data.code === 20000) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }
        })
      }
    }
  }
</script>
<style lang="scss">
.el-radio-button{
  span{
    min-width: 100px;
    max-width: 200px;
  }
  
}
.radio_button{
 text-align: center;
}

</style>
